<?php
//-------------------------------------------------------------------------------------------------------
    require_once($config['app_dir'] . '/classes/sparkline.data.php');
    require_once($config['app_dir'] . '/includes/template.class.php');
    require_once($config['app_dir'] . '/includes/list.class.php');
    require_once($config['app_dir'] . '/includes/session.class.php');
    require_once($config['app_dir'] . '/includes/date.class.php');
    require_once($config['app_dir'] . '/classes/sparklines.deamon.php');
    //---------------------------------------------------------------------------------------------------
    class objSparkLineOutServer extends objLastSparklineOutDeamon{
	        public $max_items = 3;
            public $system = false;
	        protected $pair = false;
            protected $pairs = array();
            protected $_sql_querys = array();

        protected function Prepare() {
            $this->pairs = objDataBase::GetInstance()->handler->CacheGetAll(3*60, $this->_sql_querys['sparkline']);//3 minute cache
        } //Prepare

        public function Render() {
          global $config;
            $this->Prepare();
            $cnter=1;
            $text = '<table width="100%" class="sparkline" border="0" cellpadding="0" cellspacing="0">';
            foreach($this->pairs as $pair) {
                $id = $this->GenerateID($pair['TradeID'].'_'.$pair['Start'].'_'.$pair['Finish'].'_'.$pair['SystemID']/*$this->system*/ );
                //$this->GenerateID($pair['TradeID'].' '.$pair['Start'].' '.$pair['Finish'].' '.$system);

                $Item = new objTemplate();
                $Item->caching = 2;
                $Item->cache_lifetime = -1;

                if( !$Item->is_cached('_SparkLineItem.tpl', $id) ) {
                    $this->renderItem($pair, $pair['SystemID']/*$this->system*/);
                }
                $text .= '<tr>' /*.$pair['TradeID'].'_'.$pair['Start'].'_'.$pair['Finish'].'_'.$pair['SystemID']*/. $Item->Fetch('_SparkLineItem.tpl', $id) .'</tr>';
                $cnter++;
            } //foreach pairs
            $text .= '</table>';

            return $text;
        } //Render

        protected function _sqls() {
            $this->_sql_querys['sparkline'] =

                            " SELECT t.TradeID,  " .
                                   " t.Pair,  " .
                                   " s.SystemID,  " .
                                   " s.TimeStart,  " .
      				               " s.TimeFinish,  " .
                                   " FROM_UNIXTIME(s.TimeStart, '%Y-%m-%d') AS Start,  " .
                                   " FROM_UNIXTIME(s.TimeFinish, '%Y-%m-%d') AS Finish  " .
                             " FROM  " .
                                " (SELECT tt.TradeID, tt.Pair,  " .
                                         " ( SELECT  tts.SparklinesID  " .
                                           " FROM fa_sparklines tts  " .
                                           " WHERE tts.TradeID = tt.TradeID " .
                                           " ORDER BY tts.TradeID,rand() " .
                                           " LIMIT 1  " .
                                    " ) AS SparkLinesID  " .
                                 " FROM fa_trades tt " .
								 " WHERE  " .
 							 		  " ( SELECT  tts.SparklinesID  " .
                                           " FROM fa_sparklines tts  " .
                                           " WHERE tts.TradeID = tt.TradeID " .
                                           " ORDER BY tts.TradeID,rand()  " .
                                           " LIMIT 1  " .
                                       " ) is not null " .
                                 " ORDER BY rand() " .
                                 " LIMIT $this->max_items " .
                                 " ) t  " .
                                 " LEFT OUTER JOIN " .
                                 " ( SELECT  ts.* " .
                                   " FROM fa_sparklines ts " .
                                    " ) s ON (t.TradeID=s.TradeID AND t.SparkLinesID=s.SparkLinesID)";


        }//sqls

    } //class SparkLineOutServer
    //---------------------------------------------------------------------------------------------------

    class objSparkLineInServer extends objLastSparklineInDeamon {
            public $max_items = 3;

        protected function BuildPairs() {
            $this->pairs = objDataBase::GetInstance()->handler->GetAll($this->_sql_querys['trades']);
        } //BuildPairs

        protected function Prepare() {
            $this->BuildPairs();
        } //Prepare

        public function Render() {
          global $config;
            $this->Prepare();

            $fempty=true;
            $text = '<table width="100%" class="sparkline" border="0" cellpadding="0" cellspacing="0">';
            foreach($this->pairs as $itemPair) {

                $id = $this->GenerateID($itemPair['TradeID'] . '_' . $itemPair['SystemID']);
                $SparkLineItem = new objTemplate();
                $SparkLineItem->caching = 2;
                $SparkLineItem->cache_lifetime = $this->caching_time;

                $flag = true;
                if( !$SparkLineItem->is_cached('_SparkLineItem.tpl', $id) ) {
                    //$this->BuildPeriods(); //XXX why did it use here
                    if( $flag = $this->RenderItem($itemPair['TradeID'], true, $itemPair['SystemID']) )  $fempty=false;
                } else $fempty=false;
                if( $flag ) $text .= '<tr>' . $SparkLineItem->Fetch('_SparkLineItem.tpl', $id) . '</tr>';
            } //foreach

            if( $fempty ) {
                $pair = 2400103;
                $system = 1;//$this->system;
                $id = $this->GenerateID($pair, $system);
                $SparkLineItem = new objTemplate();
                $SparkLineItem->caching = 2;
                $SparkLineItem->cache_lifetime = $this->caching_time;

                $flag = false;
                if( !$SparkLineItem->is_cached('_SparkLineItem.tpl', $id) ) {
                    //$this->BuildPeriods();

                    $item = $this->RenderItem($pair, false, $system) ;
                } //if not cached
                else $item = $SparkLineItem->Fetch('_SparkLineItem.tpl', $id);

                $text .= "<tr>$item</tr>";
            }//if empty

            $text .= '</table>';

            return $text;
        } //Render

        protected function _sqls() {
            parent::_sqls();
            //-------------------------------------------------------------------------------------------------------------------
            $this->_sql_querys['trades'] =
                           " SELECT  TradeID, SystemID FROM fa_trades_systems ORDER BY rand() LIMIT $this->max_items";
                           //"SELECT TradeID FROM fa_trades t ";
        }  //_sqls

    } //class objSparkLineInServer

    //---------------------------------------------------------------------------------------------------

    function showSparkLinesOut($system=false){
        $SparkLineServer = new objSparkLineOutServer();
        $SparkLineServer->system = $system;
        //----------------------------------------------------------------------
        $Response = new xajaxResponse();
        $Response->addAssign("divSparklines","innerHTML", $SparkLineServer->Render());
        //----------------------------------------------------------------------
        return $Response;
    } //showSparkLinesOut

    function showSparkLinesIn($system=false) {
        $SparkLineServer = new objSparkLineInServer();
        $SparkLineServer->system = $system;
        //----------------------------------------------------------------------
        $Response = new xajaxResponse();
        $Response->addAssign("divSparklines","innerHTML", $SparkLineServer->Render());
        //----------------------------------------------------------------------
        return $Response;
    } //showSparkLinesIn
//-------------------------------------------------------------------------------------------------------
?>
